using System;
using System.Data;


/// <summary>
/// Summary description for UserLogin
/// </summary>
public partial class RecruitVietnam
{

    #region Login Authentication
    public string[] LoginAuthentication(
	string email, string password, string type)
	{
	    errorMessage="";
        string[] valueToGet = null;
	    try
	    {
	        SqlServerProvider sqlServerProvider
	        = new SqlServerProvider(connectionString);
	        sqlServerProvider.CommandText = "SPLogin";
	        sqlServerProvider.CommandType = 
	        CommandType.StoredProcedure;
	        sqlServerProvider.ParameterCollection = 
	        new string[2] {"@Type", "@Email"};
	        sqlServerProvider.ValueCollection = 
	        new string[2] {type, email};
	        valueToGet = sqlServerProvider.GetStrings();
            sqlServerProvider.CloseConnection();
            if (valueToGet != null )
	        {
                SecurityProvider securityProvider 
                = new SecurityProvider();
                if (valueToGet[0] ==
                securityProvider.RCVEncPwd(password))
	            {
	                valueToGet[0] = "OK";	                 
                }
                else
                    valueToGet[0] = "P";
	        }
	        else
	        {
                valueToGet[0] = "U";
	        }	               
	    }
	    catch(Exception ex)
	    {
	        errorMessage = ex.Message;
	    }
	    return valueToGet;
    }
    #endregion

    #region ChangePassword
    public bool ChangePassword(
    string email, string OldPassword, 
    string newPassword, string type)
    {
        errorMessage = "";
        bool changePassword=true;
        try
        {
            SecurityProvider securityProvider 
            = new SecurityProvider();
            SqlServerProvider sqlServerProvider
                = new SqlServerProvider(connectionString);
            sqlServerProvider.CommandText = "SPChangePassword";
            sqlServerProvider.CommandType =
            CommandType.StoredProcedure;
            sqlServerProvider.ParameterCollection =
            new string[4] { "@Type", "@Email", 
            "@OldPassword", "@NewPassword"};
            sqlServerProvider.ValueCollection =
            new string[4] { type, email, 
            securityProvider.RCVEncPwd(OldPassword), 
            securityProvider.RCVEncPwd(newPassword)};
            sqlServerProvider.ReturnValueParameter = "@ReturnValue";
            sqlServerProvider.ExecuteNonQuery();
            changePassword = (Convert.ToString(
            sqlServerProvider.ReturnValue) == "1");
            sqlServerProvider.CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return changePassword;
    }

    #endregion

    #region GetPassword
    public string GetPassword(
    string email, string fullName, 
    string dateOfBirth, string type)
    {
        errorMessage = "";
        string forgotPassword = "";
        try
        {           
            SqlServerProvider sqlServerProvider
                = new SqlServerProvider(connectionString);
            sqlServerProvider.CommandText = "SPForgotPassword";
            sqlServerProvider.CommandType =
            CommandType.StoredProcedure;
            sqlServerProvider.ParameterCollection =
            new string[4] { "@Type", "@Email", 
            "@FullName", "@DateOfBirth"};
            sqlServerProvider.ValueCollection =
            new string[4] { type, email,  
            fullName, dateOfBirth};
            forgotPassword = Convert.ToString(
            sqlServerProvider.ExecuteScalar());
            sqlServerProvider.CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return forgotPassword;
    }
    #endregion

    #region Administration Login Authentication
    public string[] AdminLogin(
    string userName, string password)
    {
        errorMessage = "";
        string[] valueToGet = null;
        try
        {
            SqlServerProvider sqlServerProvider
            = new SqlServerProvider(connectionString);
            sqlServerProvider.CommandText = "SPAdminLogin";
            sqlServerProvider.CommandType =
            CommandType.StoredProcedure;
            sqlServerProvider.ParameterCollection =
            new string[1] { "@UserName" };
            sqlServerProvider.ValueCollection =
            new string[1] { userName };
            valueToGet = sqlServerProvider.GetStrings();
            sqlServerProvider.CloseConnection();
            if (valueToGet != null && valueToGet[0] != null)
            {
                SecurityProvider securityProvider
                = new SecurityProvider();
                if (valueToGet[0] ==
                securityProvider.RCVEncPwd(password))
                {
                    valueToGet[0] = "OK";
                }
                else
                    valueToGet[0] = "P";
            }
            else
            {
                valueToGet[0] = "U";
            }             
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return valueToGet;
    }

    public string[] WindowsLoginAuthentication()
    {
        errorMessage = "";
        SqlServerProvider sqlServerProvider = null;
        string[] valueToGet = new string[1];
        try
        {
            sqlServerProvider
            = new SqlServerProvider(connectionString);
            valueToGet[0] = "OK";                            
        }
        catch (Exception ex)
        {            
            valueToGet[0] = "U";
            errorMessage = ex.Message;
        }
        return valueToGet;
    }
    #endregion
     
}
 